package com.edushare.common.mailer;

import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.OutgoingEmails;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class Message extends MailStringLoader {

	public Message(Users to, Users from, String message, Universities univ){
		userTo = to;
		init(from, message, univ.getUniversityName(), univ);
	}
	
	public Message(Users to, Users from, String message, Departments dept){
		userTo = to;
		init(from, message, dept.getDeptName(), dept);
	}
	
	public Message(Users to, Users from, String message, CourseInstances inst){
		userTo = to;
		init(from, message, inst.getCourse().getCourseName(), inst);
	}
	
	public Message(Users to, Users from, String message){
		userTo = to;
		init(from, message, "you", to);
	}
	
	private void init(Users from, String message, String entityName, Object entity){
		replaceMap.put("CUSTOM_MESSAGE",message);
		replaceMap.put("FROM_USER_URL",URLUtils.getHomePageURL(from));
		replaceMap.put("FROM_USER_NAME",from.getCompleteUserName());
		replaceMap.put("ENTITY_URL",URLUtils.getHomePageURL(entity));
		replaceMap.put("ENTITY_NAME",entityName);
		replaceMap.put("USER_URL",URLUtils.getHomePageURL(userTo));
		messageString = templateParser(mailString, replaceMap);
	}
	
	private Users userTo = null;
	private String mailString = MailBase.get().stringMap.get(MailBase.MESSAGE_MAIL);
	
	@Override
	public OutgoingEmails send() {
		OutgoingEmails mail = new OutgoingEmails();
		mail.setContent(messageString);
		mail.setReceiverEmailId(userTo.getEmail());
		mail.setStatus(OutgoingEmails.EMAIL_TO_BE_SENT);
		mail.setCopyType(1);
		mail.setSubject("EduShare Messaging Service");
		return mail;
	}
}
